package com.szqhyxj.youxiangju.biz.creditCardEvaluation;

import com.szqhyxj.youxiangju.biz.card.CardModel;
import com.szqhyxj.youxiangju.biz.core.UserInfoManger;
import com.szqhyxj.youxiangju.biz.personalInformation.PersonalInformationModel;
import com.szqhyxj.youxiangju.sys.BaseResult;
import com.szqhyxj.youxiangju.sys.util.PhonenumberUtil;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.schedulers.Schedulers;

import static com.szqhyxj.youxiangju.sys.util.DesensitizationUtils.idEncrypt;

/**
 * Created by leung on 2018/11/3.
 */

public class CreaditCardEcaluationPresenter implements CreaditCardEcaluationContract.Presenter {
    CreaditCardEcaluationContract.View view;
    private List<Disposable> disposables = new ArrayList<>();
    CreaditCardEcaluationModel.CreaditCardEcaluationEntity creaditCardEcaluationEntity;

    public CreaditCardEcaluationPresenter(CreaditCardEcaluationContract.View view) {
        this.view = view;
        creaditCardEcaluationEntity = new CreaditCardEcaluationModel.CreaditCardEcaluationEntity();
    }

    @Override
    public void onDestroy() {

    }

    @Override
    public void setPhone(String s) {
        creaditCardEcaluationEntity.setPhone(s);
        checkRegisterEnable();
    }

    @Override
    public void setCardId(String s) {
        creaditCardEcaluationEntity.setCardId(s);
        checkRegisterEnable();
    }

    @Override
    public void setsms(String s) {
        creaditCardEcaluationEntity.setReferralCode(s);
        checkRegisterEnable();
    }

    @Override
    public void checkSms(String s) {
        CreaditCardEcaluationModel.getsmsAuthorizationCheck(s)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseResult>() {
                    @Override
                    public void accept(BaseResult baseResult) throws Exception {
                        if (baseResult.businessLogicSuccess()) {
                            view.checkSuccess();
                        } else {
                            view.showToast(baseResult.getMessage());
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showToast(throwable.getMessage());
                    }
                });
    }

    @Override
    public void init() {
        PersonalInformationModel.getpersonalInformation()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseResult<PersonalInformationModel.PersonalInformationEntity>>() {
                    @Override
                    public void accept(BaseResult<PersonalInformationModel.PersonalInformationEntity> personalInformationEntityBaseResult) throws Exception {
                        if (personalInformationEntityBaseResult.businessLogicSuccess()) {
                            String idcard = idEncrypt(personalInformationEntityBaseResult.getData().getIdCard().toString());
                            view.showCardId(idcard);

                        } else {
                            view.showToast(personalInformationEntityBaseResult.getMessage());
                        }


                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showToast(throwable.getMessage());
                    }
                });
    }

    @Override
    public void setUserInfo() {
        view.showInfo(UserInfoManger.INSTANCE.getUser().getUsername());
    }

    @Override
    public void setAgree(Boolean b) {
        creaditCardEcaluationEntity.setAgree(b);
        checkRegisterEnable();
    }

    private void checkRegisterEnable() {
        if (creaditCardEcaluationEntity.valid()) {
            view.showinquire();

        } else {
            view.showenInquire();

        }
    }

    @Override
    public void selectcard() {
        CardModel.getCards()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<BaseResult<List<CardModel.Card>>>() {
                    @Override
                    public void accept(BaseResult<List<CardModel.Card>> listBaseResult) throws Exception {
                        if (listBaseResult.businessLogicSuccess()) {

                            List<CardModel.Card> list = listBaseResult.getData();
                            view.showCardList(list);
                        } else {
                            view.showFail(listBaseResult.getMessage());
                        }

                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        view.showFail(throwable.getMessage());
                    }
                });
    }

    private static final int MINUTE = 60;

    @Override
    public void acquireVerificationCode() {
        if (creaditCardEcaluationEntity.getCardId() == null || creaditCardEcaluationEntity.getCardId().equals("")) {
            view.showToast("请选择信用卡！");
        }
        if (!PhonenumberUtil.isMobileNO(creaditCardEcaluationEntity.getPhone())) {
            view.showPhoneNumberFail();
        } else {
            view.showAcquiringVerificationCode(MINUTE);
            CreaditCardEcaluationModel.getsms(this.creaditCardEcaluationEntity)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe(
                            new Consumer<BaseResult>() {
                                @Override
                                public void accept(BaseResult baseResult) {
                                    if (baseResult.businessLogicSuccess()) {
                                        view.showAcquiredVerificationCodeSuccess();
                                        startCountDown();
                                    } else {
                                        view.showAcquiredVerificationCodeFail(baseResult.getMessage());
                                    }
                                }
                            },
                            new Consumer<Throwable>() {
                                @Override
                                public void accept(Throwable throwable) {
                                    view.showAcquiredVerificationCodeFail(throwable.getMessage());
                                }
                            }
                    );
        }


    }


    private void startCountDown() {
        disposables.add(Observable
                .interval(0, 1, TimeUnit.SECONDS)
                .take(MINUTE)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Consumer<Long>() {
                    @Override
                    public void accept(Long aLong) {

                        if (aLong == MINUTE - 1) {
                            view.showAcquiredVerificationCode();
                        } else {
                            view.showAcquiringVerificationCode((int) (MINUTE - aLong - 1));
                        }
                    }
                }));
    }
}
